import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { IS_SYSTEM } from '../../../utils/constant';
import { Button, message } from 'antd';
import services from '../../../services/api';
import Modal from '../../../components/Modal/index';
import Enumeration from './enumeration';

class FieldList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'vars';
    this.state = {
      applications: [],
      events: [],
    };
    this.operatorWidth = '120px';
    this.enumerationButton = (record) => {
      const reg = /^sys_\w+/;
      const disabled = record.is_system === IS_SYSTEM._TRUE && !reg.test(record.field_code);
      return (
        <Button
          size='small'
          type='link'
          onClick={this.enumerationModel.bind(this, record)}
          disabled={disabled || record.audit_status === this.AUDIT._WAIT}
        >
          枚举
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.enumerationButton];
    this.enumerationRef = React.createRef();
  }

  enumerationModel(record) {
    Modal.open({
      title: '枚举设置',
      width: '600px',
      content: <Enumeration id={record.id} createRef={this.enumerationRef} />,
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        this.enumerationRef.current.getValue((data) => {
          const params = {
            id: record.id,
            varEnumVos: data,
          };
          services[this.api].updateVarEnum(params).then((data) => {
            if (data.code === '0') {
              message.success(data.msg);
              Modal.close();
            } else {
              message.error(data.msg);
            }
          });
        });
      },
      cancel: () => {
        Modal.close();
      },
    });
  }
}

export default FieldList;
